// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/user/api/SysOrganApi'

import ComClass from '@api/ComQuery'
import TreeClass from '@api/user/model/SysOrganAndUserReq'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 结构树 —————————————————————————————— //

  async getTree(treeParam, TIP = `查询${CORE.MAP.ORGAN}和用户`) {
    const DTO = CORE.grpcDTO(TreeClass, treeParam, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'sysOrganAndUserQueryNew', DTO)
    return resp.content || []
  },
  transTree(arr) {
    return arr.map(({ deptId, deptName, child, content }) => {
      const childList = this.transTree(child)

      const userList = content.map((user) => ({
        children: [],
        id: `${deptId}-${user.userId}`,
        name: user.userName || '未知',
        deptId,
        user,
      }))

      return {
        id: deptId,
        name: deptName,
        deptNum: childList.length,
        userNum: userList.length,
        children: [...userList, ...childList],
      }
    })
  },
}
